/* eslint-disable indent */
import * as ${classObjectName}Service from '../services/${className}Service'
import { message } from 'antd';
import queryString from 'query-string';

export default {
    namespace: '${classObjectName}',
    state: {
        page:{
            pageData:[],
            pageSize:0,
            pageNum:0,
            total:0
        },
        record:Object,
        selectedRowKeys:[],
        addVisible:false,
        editVisible:false
    },
    reducers: {
        pages(state, { payload:{page}}) {
            return { ...state, page};
        },
        handleSelect(state,{payload:{selectedRowKeys}}){
            return {...state,selectedRowKeys:selectedRowKeys}
        },
        rowSelection(state,{payload:{selectedRowKeys}}){
            return {...state,selectedRowKeys:selectedRowKeys}
        },
        handleAddModelState(state,{payload:{addVisible}}){
            return {...state,addVisible:addVisible}
        },
        handleEditModelState(state,{payload:{editVisible}}){
            return {...state,editVisible:editVisible}
        },
        setDetail(state,{payload:{record}}){
            return {...state,record:record}
        }
    },
    effects: {
        *getPages({ payload: { pageNum = 1,pageSize=10 } }, { call, put }){
            const page = yield call(${classObjectName}Service.queryPage,{pageNum,pageSize});
            yield put({type: 'pages', payload: {page}});
        },
        *add({payload:{values}},{call,put,select}){
            yield call(${classObjectName}Service.add,values);
            const {pageNum,pageSize} = yield select(state => state.${classObjectName}.page); // 从state获取原来的分页数据
            yield put({ type: 'getPages', payload: { pageNum,pageSize} });
        },
        *delete({payload:{selectedRowKeys}},{call,put,select}){
            yield call(${classObjectName}Service.bathDelete,{ids:selectedRowKeys});
            const {pageNum,pageSize} = yield select(state => state.${classObjectName}.page); // 从state获取原来的分页数据
            yield put({ type: 'getPages', payload: { pageNum,pageSize} });
        },
        *detail({ payload: {id}}, { call, put }){
            const data = yield call(${classObjectName}Service.queryDetail,{id});
            const record = data;
            yield put({ type: 'setDetail', payload: {record : data} });
        }
    },
    subscriptions: {
    // 类似与window.location.onload
    setup({ dispatch, history }) {
        return history.listen(({ pathname, search }) => {
            const query = queryString.parse(search);
            if (pathname === '/${classObjectName}') {
                dispatch({ type: 'getPages', payload: query });
            }
        });
        }
    }
};
